#include "widget.h"
#include "ui_widget.h"

const QString SERVER_IP = "127.0.0.1";
const quint16 SERVER_PORT = 9090;


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setWindowTitle("客户端");

    udpSocket = new QUdpSocket(this);

    connect(udpSocket,&QUdpSocket::readyRead, this, &Widget::precessResponse);
}

Widget::~Widget()
{
    delete ui;
}

QString Widget::precessResponse()
{
    QNetworkDatagram responseDatagram = udpSocket->receiveDatagram();

    QString response = responseDatagram.data();

    QString log = "响应："+response;

    ui->listWidget->addItem(log);
}


void Widget::on_pushButton_clicked()
{
    QString text = ui->lineEdit->text();

    QNetworkDatagram requestDatagram(text.toUtf8(), QHostAddress(SERVER_IP), SERVER_PORT);

    udpSocket->writeDatagram(requestDatagram);

    ui->listWidget->addItem("客户端说："+text);

    ui->lineEdit->setText("");
}
























